/**
 * MainMenuBar.java
 * @author: liuex
 * 2008-1-12 ����01:59:01
 *
 * note: 
 */
package lex.lianliankan.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import org.lex.swing.ButtonFactory;

import lex.lianliankan.Lianliankan;

public class MainMenuBar extends JMenuBar implements ActionListener
{
	private static final String StartAC = "&Start";
	private static final String ExitAC = "E&xit";
	private static final String HintAC = "H&int";
	private static final String ShuffleAC = "S&huffle";
	private static final String PauseAC = "&Pause";
	private static final String AboutAC = "&About";
	// ===================================
	private JMenu gameMenu = null;
	private JMenuItem startMenuItem = null;
	private JMenuItem exitMenuItem = null;
	// ===================================
	private JMenu toolMenu = null;
	private JMenuItem hintMenuItem = null;
	private JMenuItem shuffleMenuItem = null;
	private JMenuItem pauseMenuItem = null;
	// ===================================
	private JMenu helpMenu = null;
	private JMenuItem aboutMenuItem = null;

	public MainMenuBar()
	{
		this.add( this.getGameMenu() );
		this.add( this.getToolMenu() );
		this.add( this.getHelpMenu() );
	}

	private Lianliankan getApplication()
	{
		return Lianliankan.getApplication();
	}

	public void actionPerformed( ActionEvent e )
	{
		String ac = e.getActionCommand();
		if( ac == MainMenuBar.StartAC )
		{
			this.getStartMenuItem().setEnabled( false );
			this.getHintMenuItem().setEnabled( true );
			this.getShuffleMenuItem().setEnabled( true );
			this.getPauseMenuItem().setEnabled( true );
			this.getApplication().getContentPane().start();
		}
		else if( ac == MainMenuBar.ExitAC )
		{
			Lianliankan.getApplication().exit();
		}
		else if( ac == MainMenuBar.HintAC )
		{
			// if( main.getModel().findPath() != null )
			// (new HighliteHintThread( main )).start();
			// else
			// {
			// JOptionPane.showMessageDialog( main, "�Ҳ�������·��!" );
			// }
		}
		else if( ac == MainMenuBar.ShuffleAC )
		{
			this.getApplication().getContentPane().shuffle();
		}
		else if( ac == MainMenuBar.PauseAC )
		{
			this.getApplication().getContentPane().pause();
		}
		if( ac == MainMenuBar.AboutAC )
		{
			JOptionPane.showMessageDialog( this.getApplication().getRootWindow(), "HA!", "About",
				JOptionPane.INFORMATION_MESSAGE );
		}
	}

	private JMenu getGameMenu()
	{
		if( null == gameMenu )
		{
			gameMenu = ButtonFactory.newMenu( "&Game" );
			gameMenu.add( this.getStartMenuItem() );
			gameMenu.add( this.getExitMenuItem() );
		}
		return gameMenu;
	}

	private JMenu getToolMenu()
	{
		if( null == toolMenu )
		{
			toolMenu = ButtonFactory.newMenu( "&Tool" );
			toolMenu.add( this.getHintMenuItem() );
			toolMenu.add( this.getShuffleMenuItem() );
			toolMenu.add( this.getPauseMenuItem() );
		}
		return toolMenu;
	}

	@Override
	public JMenu getHelpMenu()
	{
		if( null == helpMenu )
		{
			helpMenu = ButtonFactory.newMenu( "&Help" );
			helpMenu.add( this.getAboutMenuItem() );
		}
		return helpMenu;
	}

	private JMenuItem getAboutMenuItem()
	{
		if( null == this.aboutMenuItem )
		{
			aboutMenuItem = ButtonFactory.newMenuItem( MainMenuBar.AboutAC );
			aboutMenuItem.addActionListener( this );
		}
		return aboutMenuItem;
	}

	private JMenuItem getStartMenuItem()
	{
		if( null == this.startMenuItem )
		{
			startMenuItem = ButtonFactory.newMenuItem( MainMenuBar.StartAC );
			startMenuItem.setAccelerator( KeyStroke.getKeyStroke( 113, 0 ) );
			startMenuItem.addActionListener( this );
		}
		return startMenuItem;
	}

	private JMenuItem getHintMenuItem()
	{
		if( null == hintMenuItem )
		{
			hintMenuItem = ButtonFactory.newMenuItem( MainMenuBar.HintAC );
			hintMenuItem.setAccelerator( KeyStroke.getKeyStroke( 116, 0 ) );
			hintMenuItem.addActionListener( this );
			hintMenuItem.setEnabled( false );
		}
		return hintMenuItem;
	}

	private JMenuItem getShuffleMenuItem()
	{
		if( null == shuffleMenuItem )
		{
			shuffleMenuItem = ButtonFactory.newMenuItem( MainMenuBar.ShuffleAC );
			shuffleMenuItem.addActionListener( this );
			shuffleMenuItem.setEnabled( false );
		}
		return shuffleMenuItem;
	}

	private JMenuItem getPauseMenuItem()
	{
		if( null == pauseMenuItem )
		{
			pauseMenuItem = ButtonFactory.newMenuItem( MainMenuBar.PauseAC );
			pauseMenuItem.addActionListener( this );
			pauseMenuItem.setEnabled( false );
		}
		return pauseMenuItem;
	}

	private JMenuItem getExitMenuItem()
	{
		if( null == this.exitMenuItem )
		{
			exitMenuItem = ButtonFactory.newMenuItem( MainMenuBar.ExitAC );
			exitMenuItem.addActionListener( this );
		}
		return exitMenuItem;
	}
}
